package com.ttbj.util;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.ttbj.constant.FlagEnum;
import com.ttbj.dto.ViewData;

import java.util.List;


public class ViewDataUtil {
    private static final Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
    private static ViewData viewData = null;

    public static String buildSuccessJson(String message) {
        viewData = new ViewData();
        viewData.setMessage(message);
        viewData.setStatus(FlagEnum.SUCCESS.code());
        return gson.toJson(viewData);
    }

    public static String buildSuccessJson(String message, Object data) {
        viewData = new ViewData();
        viewData.setMessage(message);
        viewData.setStatus(FlagEnum.SUCCESS.code());
        viewData.setData(data);
        return gson.toJson(viewData);
    }

    public static String buildSuccessPageJson(String message, List data, Integer total) {
        viewData = new ViewData();
        viewData.setMessage(message);
        viewData.setStatus(FlagEnum.SUCCESS.code());
        viewData.setRows(data);
        viewData.setTotal(total);
        return gson.toJson(viewData);
    }

    public static String buildSuccessJson(int code, String message, Object data) {
        viewData = new ViewData();
        viewData.setStatus(code);
        viewData.setMessage(message);
        viewData.setData(data);
        return gson.toJson(viewData);
    }

    public static String buildFailureJson(int code, String message) {
        viewData = new ViewData();
        viewData.setStatus(code);
        viewData.setMessage(message);
        return gson.toJson(viewData);
    }


}
